package main

import (
	"fmt"
	"runtime"
	"sync"
	"sync/atomic"
)

var salay int32 = 0

// 10个例程 分别给 salay + 10 1000次
// 10个例程 分别给 salay - 10 1000次
func main() {
	fmt.Println("start")
	var wg = new(sync.WaitGroup)

	for i := 0; i < 10; i++ {
		wg.Add(2)
		go func() {
			defer wg.Done()
			for i := 0; i < 1000; i++ {
				atomic.AddInt32(&salay, +10)
				runtime.Gosched()
			}
		}()

		go func() {
			defer wg.Done()
			for i := 0; i < 1000; i++ {
				atomic.AddInt32(&salay, -10)
				runtime.Gosched()
			}
		}()
	}
	wg.Wait()
	fmt.Println("end:", salay)
}
